home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perldata.Z / perldata
Encoding:
Text File  |  1998-10-28  |  32.5 KB  |  859 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perldata - Perl data types
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.       VVVVaaaarrrriiiiaaaabbbblllleeee nnnnaaaammmmeeeessss
  13.  
  14.       Perl has three data structures: scalars, arrays of scalars,
  15.       and associative arrays of scalars, known as "hashes".
  16.       Normal arrays    are indexed by number, starting    with 0.
  17.       (Negative subscripts count from the end.)  Hash arrays are
  18.       indexed by string.
  19.  
  20.       Values are usually referred to by name (or through a named
  21.       reference).  The first character of the name tells you to
  22.       what sort of data structure it refers.  The rest of the name
  23.       tells    you the    particular value to which it refers.  Most
  24.       often, it consists of    a single _i_d_e_n_t_i_f_i_e_r, that is, a    string
  25.       beginning with a letter or underscore, and containing
  26.       letters, underscores,    and digits.  In    some cases, it may be
  27.       a chain of identifiers, separated by :: (or by ', but    that's
  28.       deprecated); all but the last    are interpreted    as names of
  29.       packages, to locate the namespace in which to    look up    the
  30.       final    identifier (see    the Packages entry in the _p_e_r_l_m_o_d
  31.       manpage for details).     It's possible to substitute for a
  32.       simple identifier an expression that produces    a reference to
  33.       the value at runtime;    this is    described in more detail
  34.       below, and in    the _p_e_r_l_r_e_f manpage.
  35.  
  36.       There    are also special variables whose names don't follow
  37.       these    rules, so that they don't accidentally collide with
  38.       one of your normal variables.     Strings that match
  39.       parenthesized    parts of a regular expression are saved    under
  40.       names    containing only    digits after the $ (see    the _p_e_r_l_o_p
  41.       manpage and the _p_e_r_l_r_e manpage).  In addition, several
  42.       special variables that provide windows into the inner
  43.       working of Perl have names containing    punctuation characters
  44.       (see the _p_e_r_l_v_a_r manpage).
  45.  
  46.       Scalar values    are always named with '$', even    when referring
  47.       to a scalar that is part of an array.     It works like the
  48.       English word "the".  Thus we have:
  49.  
  50.           $days          # the    simple scalar value "days"
  51.           $days[28]          # the    29th element of    array @days
  52.           $days{'Feb'}      # the    'Feb' value from hash %days
  53.           $#days          # the    last index of array @days
  54.  
  55.       but entire arrays or array slices are    denoted    by '@',    which
  56.       works    much like the word "these" or "those":
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  71.  
  72.  
  73.  
  74.           @days          # ($days[0], $days[1],... $days[n])
  75.           @days[3,4,5]      # same as @days[3..5]
  76.           @days{'a','c'}      # same as ($days{'a'},$days{'c'})
  77.  
  78.       and entire hashes are    denoted    by '%':
  79.  
  80.           %days          # (key1, val1, key2, val2 ...)
  81.  
  82.       In addition, subroutines are named with an initial '&',
  83.       though this is optional when it's otherwise unambiguous
  84.       (just    as "do"    is often redundant in English).     Symbol    table
  85.       entries can be named with an initial '*', but    you don't
  86.       really care about that yet.
  87.  
  88.       Every    variable type has its own namespace.  You can, without
  89.       fear of conflict, use    the same name for a scalar variable,
  90.       an array, or a hash (or, for that matter, a filehandle, a
  91.       subroutine name, or a    label).     This means that $foo and @foo
  92.       are two different variables.    It also    means that $foo[1] is
  93.       a part of @foo, not a    part of    $foo.  This may    seem a bit
  94.       weird, but that's okay, because it is    weird.
  95.  
  96.       Because variable and array references    always start with '$',
  97.       '@', or '%', the "reserved" words aren't in fact reserved
  98.       with respect to variable names.  (They ARE reserved with
  99.       respect to labels and    filehandles, however, which don't have
  100.       an initial special character.     You can't have    a filehandle
  101.       named    "log", for instance.  Hint: you    could say
  102.       open(LOG,'logfile') rather than open(log,'logfile').    Using
  103.       uppercase filehandles    also improves readability and protects
  104.       you from conflict with future    reserved words.)  Case _I_S
  105.       significant--"FOO", "Foo", and "foo" are all different
  106.       names.  Names    that start with    a letter or underscore may
  107.       also contain digits and underscores.
  108.  
  109.       It is    possible to replace such an alphanumeric name with an
  110.       expression that returns a reference to an object of that
  111.       type.     For a description of this, see    the _p_e_r_l_r_e_f manpage.
  112.  
  113.       Names    that start with    a digit    may contain only more digits.
  114.       Names    that do    not start with a letter, underscore, or    digit
  115.       are limited to one character,    e.g.,  $% or $$.  (Most    of
  116.       these    one character names have a predefined significance to
  117.       Perl.     For instance, $$ is the current process id.)
  118.  
  119.       CCCCoooonnnntttteeeexxxxtttt
  120.  
  121.       The interpretation of    operations and values in Perl
  122.       sometimes depends on the requirements    of the context around
  123.       the operation    or value.  There are two major contexts:
  124.       scalar and list.  Certain operations return list values in
  125.       contexts wanting a list, and scalar values otherwise.     (If
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  137.  
  138.  
  139.  
  140.       this is true of an operation it will be mentioned in the
  141.       documentation    for that operation.)  In other words, Perl
  142.       overloads certain operations based on    whether    the expected
  143.       return value is singular or plural.  (Some words in English
  144.       work this way, like "fish" and "sheep".)
  145.  
  146.       In a reciprocal fashion, an operation    provides either    a
  147.       scalar or a list context to each of its arguments.  For
  148.       example, if you say
  149.  
  150.           int( <STDIN> )
  151.  
  152.       the integer operation    provides a scalar context for the
  153.       <STDIN> operator, which responds by reading one line from
  154.       STDIN    and passing it back to the integer operation, which
  155.       will then find the integer value of that line    and return
  156.       that.     If, on    the other hand,    you say
  157.  
  158.           sort( <STDIN> )
  159.  
  160.       then the sort    operation provides a list context for <STDIN>,
  161.       which    will proceed to    read every line    available up to    the
  162.       end of file, and pass    that list of lines back    to the sort
  163.       routine, which will then sort    those lines and    return them as
  164.       a list to whatever the context of the    sort was.
  165.  
  166.       Assignment is    a little bit special in    that it    uses its left
  167.       argument to determine    the context for    the right argument.
  168.       Assignment to    a scalar evaluates the righthand side in a
  169.       scalar context, while    assignment to an array or array    slice
  170.       evaluates the    righthand side in a list context.  Assignment
  171.       to a list also evaluates the righthand side in a list
  172.       context.
  173.  
  174.       User defined subroutines may choose to care whether they are
  175.       being    called in a scalar or list context, but    most
  176.       subroutines do not need to care, because scalars are
  177.       automatically    interpolated into lists.  See the wantarray
  178.       entry    in the _p_e_r_l_f_u_n_c    manpage.
  179.  
  180.       SSSSccccaaaallllaaaarrrr vvvvaaaalllluuuueeeessss
  181.  
  182.       All data in Perl is a    scalar or an array of scalars or a
  183.       hash of scalars.  Scalar variables may contain various kinds
  184.       of singular data, such as numbers, strings, and references.
  185.       In general, conversion from one form to another is
  186.       transparent.    (A scalar may not contain multiple values, but
  187.       may contain a    reference to an    array or hash containing
  188.       multiple values.)  Because of    the automatic conversion of
  189.       scalars, operations, and functions that return scalars don't
  190.       need to care (and, in    fact, can't care) whether the context
  191.       is looking for a string or a number.
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  203.  
  204.  
  205.  
  206.       Scalars aren't necessarily one thing or another.  There's no
  207.       place    to declare a scalar variable to    be of type "string",
  208.       or of    type "number", or type "filehandle", or    anything else.
  209.       Perl is a contextually polymorphic language whose scalars
  210.       can be strings, numbers, or references (which    includes
  211.       objects).  While strings and numbers are considered pretty
  212.       much the same    thing for nearly all purposes, references are
  213.       strongly-typed uncastable pointers with builtin reference-
  214.       counting and destructor invocation.
  215.  
  216.       A scalar value is interpreted    as TRUE    in the Boolean sense
  217.       if it    is not the null    string or the number 0 (or its string
  218.       equivalent, "0").  The Boolean context is just a special
  219.       kind of scalar context.
  220.  
  221.       There    are actually two varieties of null scalars: defined
  222.       and undefined.  Undefined null scalars are returned when
  223.       there    is no real value for something,    such as    when there was
  224.       an error, or at end of file, or when you refer to an
  225.       uninitialized    variable or element of an array.  An undefined
  226.       null scalar may become defined the first time    you use    it as
  227.       if it    were defined, but prior    to that    you can    use the
  228.       _d_e_f_i_n_e_d() operator to    determine whether the value is defined
  229.       or not.
  230.  
  231.       To find out whether a    given string is    a valid    nonzero
  232.       number, it's usually enough to test it against both numeric
  233.       0 and    also lexical "0" (although this    will cause ----wwww noises).
  234.       That's because strings that aren't numbers count as 0, just
  235.       as they do in    aaaawwwwkkkk:
  236.  
  237.           if ($str == 0 && $str ne "0")  {
  238.           warn "That doesn't look like a number";
  239.           }
  240.  
  241.       That's usually preferable because otherwise you won't    treat
  242.       IEEE notations like NaN or Infinity properly.     At other
  243.       times    you might prefer to use    the POSIX::strtod function or
  244.       a regular expression to check    whether    data is    numeric.  See
  245.       the _p_e_r_l_r_e manpage for details on regular expressions.
  246.  
  247.           warn "has    nondigits"      if     /\D/;
  248.           warn "not    a natural number" unless /^\d+$/;          #    rejects    -3
  249.           warn "not    an integer"      unless /^-?\d+$/;          #    rejects    +3
  250.           warn "not    an integer"      unless /^[+-]?\d+$/;
  251.           warn "not    a decimal number" unless /^-?\d+\.?\d*$/;     #    rejects    .2
  252.           warn "not    a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
  253.           warn "not    a C float"
  254.           unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
  255.  
  256.       The length of    an array is a scalar value.  You may find the
  257.       length of array @days    by evaluating $#days, as in ccccsssshhhh.
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  269.  
  270.  
  271.  
  272.       (Actually, it's not the length of the    array, it's the
  273.       subscript of the last    element, because there is (ordinarily)
  274.       a 0th    element.)  Assigning to    $#days changes the length of
  275.       the array.  Shortening an array by this method destroys
  276.       intervening values.  Lengthening an array that was
  277.       previously shortened _N_O _L_O_N_G_E_R recovers the values that were
  278.       in those elements.  (It used to in Perl 4, but we had    to
  279.       break    this to    make sure destructors were called when
  280.       expected.)  You can also gain    some miniscule measure of
  281.       efficiency by    pre-extending an array that is going to    get
  282.       big.    (You can also extend an    array by assigning to an
  283.       element that is off the end of the array.)  You can truncate
  284.       an array down    to nothing by assigning    the null list () to
  285.       it.  The following are equivalent:
  286.  
  287.           @whatever    = ();
  288.           $#whatever = -1;
  289.  
  290.       If you evaluate a named array    in a scalar context, it
  291.       returns the length of    the array.  (Note that this is not
  292.       true of lists, which return the last value, like the C comma
  293.       operator, nor    of built-in functions, which return whatever
  294.       they feel like returning.)  The following is always true:
  295.  
  296.           scalar(@whatever)    == $#whatever -    $[ + 1;
  297.  
  298.       Version 5 of Perl changed the    semantics of $[: files that
  299.       don't    set the    value of $[ no longer need to worry about
  300.       whether another file changed its value.  (In other words,
  301.       use of $[ is deprecated.)  So    in general you can assume that
  302.  
  303.           scalar(@whatever)    == $#whatever +    1;
  304.  
  305.       Some programmers choose to use an explicit conversion    so
  306.       nothing's left to doubt:
  307.  
  308.           $element_count = scalar(@whatever);
  309.  
  310.       If you evaluate a hash in a scalar context, it returns a
  311.       value    that is    true if    and only if the    hash contains any
  312.       key/value pairs.  (If    there are any key/value    pairs, the
  313.       value    returned is a string consisting    of the number of used
  314.       buckets and the number of allocated buckets, separated by a
  315.       slash.  This is pretty much useful only to find out whether
  316.       Perl's (compiled in) hashing algorithm is performing poorly
  317.       on your data set.  For example, you stick 10,000 things in a
  318.       hash,    but evaluating %HASH in    scalar context reveals "1/16",
  319.       which    means only one out of sixteen buckets has been
  320.       touched, and presumably contains all 10,000 of your items.
  321.       This isn't supposed to happen.)
  322.  
  323.       You can preallocate space for    a hash by assigning to the
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  335.  
  336.  
  337.  
  338.       _k_e_y_s() function.  This rounds    up the allocated bucked    to the
  339.       next power of    two:
  340.  
  341.           keys(%users) = 1000;          # allocate 1024 buckets
  342.  
  343.  
  344.       SSSSccccaaaallllaaaarrrr vvvvaaaalllluuuueeee ccccoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  345.  
  346.       Numeric literals are specified in any    of the customary
  347.       floating point or integer formats:
  348.  
  349.           12345
  350.           12345.67
  351.           .23E-10
  352.           0xffff          # hex
  353.           0377          # octal
  354.           4_294_967_296      # underline for legibility
  355.  
  356.       String literals are usually delimited    by either single or
  357.       double quotes.  They work much like shell quotes: double-
  358.       quoted string    literals are subject to    backslash and variable
  359.       substitution;    single-quoted strings are not (except for "\'"
  360.       and "\\").  The usual    Unix backslash rules apply for making
  361.       characters such as newline, tab, etc., as well as some more
  362.       exotic forms.     See the section on _Q_u_o_t_e _a_n_d _Q_u_o_t_e_l_i_k_e
  363.       _O_p_e_r_a_t_o_r_s in the _p_e_r_l_o_p manpage for a    list.
  364.  
  365.       Octal    or hex representations in string literals (e.g.
  366.       '0xffff') are    not automatically converted to their integer
  367.       representation.  The _h_e_x() and _o_c_t() functions make these
  368.       conversions for you.    See the    hex entry in the _p_e_r_l_f_u_n_c
  369.       manpage and the oct entry in the _p_e_r_l_f_u_n_c manpage for    more
  370.       details.
  371.  
  372.       You can also embed newlines directly in your strings,    i.e.,
  373.       they can end on a different line than    they begin.  This is
  374.       nice,    but if you forget your trailing    quote, the error will
  375.       not be reported until    Perl finds another line    containing the
  376.       quote    character, which may be    much further on    in the script.
  377.       Variable substitution    inside strings is limited to scalar
  378.       variables, arrays, and array slices.    (In other words, names
  379.       beginning with $ or @, followed by an    optional bracketed
  380.       expression as    a subscript.)  The following code segment
  381.       prints out "The price    is $100."
  382.  
  383.           $Price = '$100';      # not    interpreted
  384.           print "The price is $Price.\n";      # interpreted
  385.  
  386.       As in    some shells, you can put curly brackets    around the
  387.       name to delimit it from following alphanumerics.  In fact,
  388.       an identifier    within such curlies is forced to be a string,
  389.       as is    any single identifier within a hash subscript.    Our
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  401.  
  402.  
  403.  
  404.       earlier example,
  405.  
  406.           $days{'Feb'}
  407.  
  408.       can be written as
  409.  
  410.           $days{Feb}
  411.  
  412.       and the quotes will be assumed automatically.     But anything
  413.       more complicated in the subscript will be interpreted    as an
  414.       expression.
  415.  
  416.       Note that a single-quoted string must    be separated from a
  417.       preceding word by a space, because single quote is a valid
  418.       (though deprecated) character    in a variable name (see    the
  419.       Packages entry in the    _p_e_r_l_m_o_d    manpage).
  420.  
  421.       Three    special    literals are __FILE__, __LINE__, and
  422.       __PACKAGE__, which represent the current filename, line
  423.       number, and package name at that point in your program.
  424.       They may be used only    as separate tokens; they will not be
  425.       interpolated into strings.  If there is no current package
  426.       (due to an empty package; directive),    __PACKAGE__ is the
  427.       undefined value.
  428.  
  429.       The tokens __END__ and __DATA__ may be used to indicate the
  430.       logical end of the script before the actual end of file.
  431.       Any following    text is    ignored, but may be read via a DATA
  432.       filehandle: main::DATA for __END__, or PACKNAME::DATA    (where
  433.       PACKNAME is the current package) for __DATA__.  The two
  434.       control characters ^D    and ^Z are synonyms for    __END__    (or
  435.       __DATA__ in a    module).  See the _S_e_l_f_L_o_a_d_e_r manpage for more
  436.       description of __DATA__, and an example of its use.  Note
  437.       that you cannot read from the    DATA filehandle    in a BEGIN
  438.       block: the BEGIN block is executed as    soon as    it is seen
  439.       (during compilation),    at which point the corresponding
  440.       __DATA__ (or __END__)    token has not yet been seen.
  441.  
  442.       A word that has no other interpretation in the grammar will
  443.       be treated as    if it were a quoted string.  These are known
  444.       as "barewords".  As with filehandles and labels, a bareword
  445.       that consists    entirely of lowercase letters risks conflict
  446.       with future reserved words, and if you use the ----wwww switch,
  447.       Perl will warn you about any such words.  Some people    may
  448.       wish to outlaw barewords entirely.  If you say
  449.  
  450.           use strict 'subs';
  451.  
  452.       then any bareword that would NOT be interpreted as a
  453.       subroutine call produces a compile-time error    instead.  The
  454.       restriction lasts to the end of the enclosing    block.    An
  455.       inner    block may countermand this by saying no    strict 'subs'.
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  467.  
  468.  
  469.  
  470.       Array    variables are interpolated into    double-quoted strings
  471.       by joining all the elements of the array with    the delimiter
  472.       specified in the $" variable ($LIST_SEPARATOR    in English),
  473.       space    by default.  The following are equivalent:
  474.  
  475.           $temp = join($",@ARGV);
  476.           system "echo $temp";
  477.  
  478.           system "echo @ARGV";
  479.  
  480.       Within search    patterns (which    also undergo double-quotish
  481.       substitution)    there is a bad ambiguity:  Is /$foo[bar]/ to
  482.       be interpreted as /${foo}[bar]/ (where [bar] is a character
  483.       class    for the    regular    expression) or as /${foo[bar]}/    (where
  484.       [bar]    is the subscript to array @foo)?  If @foo doesn't
  485.       otherwise exist, then    it's obviously a character class.  If
  486.       @foo exists, Perl takes a good guess about [bar], and    is
  487.       almost always    right.    If it does guess wrong,    or if you're
  488.       just plain paranoid, you can force the correct
  489.       interpretation with curly brackets as    above.
  490.  
  491.       A line-oriented form of quoting is based on the shell
  492.       "here-doc" syntax.  Following    a << you specify a string to
  493.       terminate the    quoted material, and all lines following the
  494.       current line down to the terminating string are the value of
  495.       the item.  The terminating string may    be either an
  496.       identifier (a    word), or some quoted text.  If    quoted,    the
  497.       type of quotes you use determines the    treatment of the text,
  498.       just as in regular quoting.  An unquoted identifier works
  499.       like double quotes.  There must be no    space between the <<
  500.       and the identifier.  (If you put a space it will be treated
  501.       as a null identifier,    which is valid,    and matches the    first
  502.       empty    line.)    The terminating    string must appear by itself
  503.       (unquoted and    with no    surrounding whitespace)    on the
  504.       terminating line.
  505.  
  506.           print    <<EOF;
  507.           The price    is $Price.
  508.           EOF
  509.  
  510.           print    <<"EOF";  # same as above
  511.           The price    is $Price.
  512.           EOF
  513.  
  514.           print    <<`EOC`;  # execute commands
  515.           echo hi there
  516.           echo lo there
  517.           EOC
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  533.  
  534.  
  535.  
  536.           print    <<"foo", <<"bar"; # you    can stack them
  537.           I    said foo.
  538.           foo
  539.           I    said bar.
  540.           bar
  541.  
  542.           myfunc(<<"THIS", 23, <<'THAT');
  543.           Here's a line
  544.           or two.
  545.           THIS
  546.           and here's another.
  547.           THAT
  548.  
  549.       Just don't forget that you have to put a semicolon on    the
  550.       end to finish    the statement, as Perl doesn't know you're not
  551.       going    to try to do this:
  552.  
  553.           print    <<ABC
  554.           179231
  555.           ABC
  556.           + 20;
  557.  
  558.  
  559.       LLLLiiiisssstttt vvvvaaaalllluuuueeee ccccoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  560.  
  561.       List values are denoted by separating    individual values by
  562.       commas (and enclosing    the list in parentheses    where
  563.       precedence requires it):
  564.  
  565.           (LIST)
  566.  
  567.       In a context not requiring a list value, the value of    the
  568.       list literal is the value of the final element, as with the
  569.       C comma operator.  For example,
  570.  
  571.           @foo = ('cc', '-E', $bar);
  572.  
  573.       assigns the entire list value    to array foo, but
  574.  
  575.           $foo = ('cc', '-E', $bar);
  576.  
  577.       assigns the value of variable    bar to variable    foo.  Note
  578.       that the value of an actual array in a scalar    context    is the
  579.       length of the    array; the following assigns the value 3 to
  580.       $foo:
  581.  
  582.           @foo = ('cc', '-E', $bar);
  583.           $foo = @foo;          # $foo gets 3
  584.  
  585.       You may have an optional comma before    the closing
  586.       parenthesis of a list    literal, so that you can say:
  587.  
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  599.  
  600.  
  601.  
  602.           @foo = (
  603.           1,
  604.           2,
  605.           3,
  606.           );
  607.  
  608.       LISTs    do automatic interpolation of sublists.     That is, when
  609.       a LIST is evaluated, each element of the list    is evaluated
  610.       in a list context, and the resulting list value is
  611.       interpolated into LIST just as if each individual element
  612.       were a member    of LIST.  Thus arrays and hashes lose their
  613.       identity in a    LIST--the list
  614.  
  615.           (@foo,@bar,&SomeSub,%glarch)
  616.  
  617.       contains all the elements of @foo followed by    all the
  618.       elements of @bar, followed by    all the    elements returned by
  619.       the subroutine named SomeSub called in a list    context,
  620.       followed by the key/value pairs of %glarch.  To make a list
  621.       reference that does _N_O_T interpolate, see the _p_e_r_l_r_e_f
  622.       manpage.
  623.  
  624.       The null list    is represented by ().  Interpolating it    in a
  625.       list has no effect.  Thus ((),(),()) is equivalent to    ().
  626.       Similarly, interpolating an array with no elements is    the
  627.       same as if no    array had been interpolated at that point.
  628.  
  629.       A list value may also    be subscripted like a normal array.
  630.       You must put the list    in parentheses to avoid    ambiguity.
  631.       For example:
  632.  
  633.           #    Stat returns list value.
  634.           $time = (stat($file))[8];
  635.  
  636.           #    SYNTAX ERROR HERE.
  637.           $time = stat($file)[8];  # OOPS, FORGOT PARENTHESES
  638.  
  639.           #    Find a hex digit.
  640.           $hexdigit    = ('a','b','c','d','e','f')[$digit-10];
  641.  
  642.           #    A "reverse comma operator".
  643.           return (pop(@foo),pop(@foo))[0];
  644.  
  645.       You may assign to undef in a list.  This is useful for
  646.       throwing away    some of    the return values of a function:
  647.  
  648.           ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
  649.  
  650.       Lists    may be assigned    to if and only if each element of the
  651.       list is legal    to assign to:
  652.  
  653.           ($a, $b, $c) = (1, 2, 3);
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  665.  
  666.  
  667.  
  668.           ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00);
  669.  
  670.       Array    assignment in a    scalar context returns the number of
  671.       elements produced by the expression on the right side    of the
  672.       assignment:
  673.  
  674.           $x = (($foo,$bar)    = (3,2,1));      # set    $x to 3, not 2
  675.           $x = (($foo,$bar)    = f());          # set    $x to f()'s return count
  676.  
  677.       This is very handy when you want to do a list    assignment in
  678.       a Boolean context, because most list functions return    a null
  679.       list when finished, which when assigned produces a 0,    which
  680.       is interpreted as FALSE.
  681.  
  682.       The final element may    be an array or a hash:
  683.  
  684.           ($a, $b, @rest) =    split;
  685.           my($a, $b, %rest)    = @_;
  686.  
  687.       You can actually put an array    or hash    anywhere in the    list,
  688.       but the first    one in the list    will soak up all the values,
  689.       and anything after it    will get a null    value.    This may be
  690.       useful in a _l_o_c_a_l() or _m_y().
  691.  
  692.       A hash literal contains pairs    of values to be    interpreted as
  693.       a key    and a value:
  694.  
  695.           #    same as    map assignment above
  696.           %map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
  697.  
  698.       While    literal    lists and named    arrays are usually
  699.       interchangeable, that's not the case for hashes.  Just
  700.       because you can subscript a list value like a    normal array
  701.       does not mean    that you can subscript a list value as a hash.
  702.       Likewise, hashes included as parts of    other lists (including
  703.       parameters lists and return lists from functions) always
  704.       flatten out into key/value pairs.  That's why    it's good to
  705.       use references sometimes.
  706.  
  707.       It is    often more readable to use the => operator between
  708.       key/value pairs.  The    => operator is mostly just a more
  709.       visually distinctive synonym for a comma, but    it also
  710.       arranges for its left-hand operand to    be interpreted as a
  711.       string--if it's a bareword that would    be a legal identifier.
  712.       This makes it    nice for initializing hashes:
  713.  
  714.           %map = (
  715.                red     => 0x00f,
  716.                blue     => 0x0f0,
  717.                green => 0xf00,
  718.          );
  719.  
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  731.  
  732.  
  733.  
  734.       or for initializing hash references to be used as records:
  735.  
  736.           $rec = {
  737.               witch    => 'Mable the Merciless',
  738.               cat    => 'Fluffy the Ferocious',
  739.               date    => '10/31/1776',
  740.           };
  741.  
  742.       or for using call-by-named-parameter to complicated
  743.       functions:
  744.  
  745.          $field = $query->radio_group(
  746.              name       => 'group_name',
  747.              values       => ['eenie','meenie','minie'],
  748.              default   => 'meenie',
  749.              linebreak => 'true',
  750.              labels       => \%labels
  751.          );
  752.  
  753.       Note that just because a hash    is initialized in that order
  754.       doesn't mean that it comes out in that order.     See the sort
  755.       entry    in the _p_e_r_l_f_u_n_c    manpage    for examples of    how to arrange
  756.       for an output    ordering.
  757.  
  758.       TTTTyyyyppppeeeegggglllloooobbbbssss aaaannnndddd    FFFFiiiilllleeeehhhhaaaannnnddddlllleeeessss
  759.  
  760.       Perl uses an internal    type called a _t_y_p_e_g_l_o_b to hold an
  761.       entire symbol    table entry.  The type prefix of a typeglob is
  762.       a *, because it represents all types.     This used to be the
  763.       preferred way    to pass    arrays and hashes by reference into a
  764.       function, but    now that we have real references, this is
  765.       seldom needed.
  766.  
  767.       The main use of typeglobs in modern Perl is create symbol
  768.       table    aliases.  This assignment:
  769.  
  770.           *this = *that;
  771.  
  772.       makes    $this an alias for $that, @this    an alias for @that,
  773.       %this    an alias for %that, &this an alias for &that, etc.
  774.       Much safer is    to use a reference.  This:
  775.  
  776.           local *Here::blue    = \$There::green;
  777.  
  778.       temporarily makes $Here::blue    an alias for $There::green,
  779.       but doesn't make @Here::blue an alias    for @There::green, or
  780.       %Here::blue an alias for %There::green, etc.    See the
  781.       section on _S_y_m_b_o_l _T_a_b_l_e_s in the _p_e_r_l_m_o_d manpage for more
  782.       examples of this.  Strange though this may seem, this    is the
  783.       basis    for the    whole module import/export system.
  784.  
  785.       Another use for typeglobs is to to pass filehandles into a
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  797.  
  798.  
  799.  
  800.       function or to create    new filehandles.  If you need to use a
  801.       typeglob to save away    a filehandle, do it this way:
  802.  
  803.           $fh = *STDOUT;
  804.  
  805.       or perhaps as    a real reference, like this:
  806.  
  807.           $fh = \*STDOUT;
  808.  
  809.       See the _p_e_r_l_s_u_b manpage for examples of using    these as
  810.       indirect filehandles in functions.
  811.  
  812.       Typeglobs are    also a way to create a local filehandle    using
  813.       the _l_o_c_a_l() operator.     These last until their    block is
  814.       exited, but may be passed back.  For example:
  815.  
  816.           sub newopen {
  817.           my $path = shift;
  818.           local    *FH;  #    not my!
  819.           open     (FH, $path)          or  return undef;
  820.           return *FH;
  821.           }
  822.           $fh = newopen('/etc/passwd');
  823.  
  824.       Now that we have the *foo{THING} notation, typeglobs aren't
  825.       used as much for filehandle manipulations, although they're
  826.       still    needed to pass brand new file and directory handles
  827.       into or out of functions. That's because *HANDLE{IO} only
  828.       works    if HANDLE has already been used    as a handle.  In other
  829.       words, *FH can be used to create new symbol table entries,
  830.       but *foo{THING} cannot.
  831.  
  832.       Another way to create    anonymous filehandles is with the
  833.       IO::Handle module and    its ilk.  These    modules    have the
  834.       advantage of not hiding different types of the same name
  835.       during the _l_o_c_a_l().  See the bottom of the open() entry in
  836.       the _p_e_r_l_f_u_n_c manpage for an example.
  837.  
  838.       See the _p_e_r_l_r_e_f manpage, the _p_e_r_l_s_u_b manpage,    and the
  839.       section on _S_y_m_b_o_l _T_a_b_l_e_s in the _p_e_r_l_m_o_d manpage for more
  840.       discussion on    typeglobs and the *foo{THING} syntax.
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.      Page 13                        (printed 10/23/98)
  856.  
  857.  
  858.  
  859.